<?php
namespace app\index\controller;

use app\backend\org\HeadTitle;
use \think\Db;
use app\backend\org\Crumbs;
use app\backend\org\Kv;

/**
 * 加入我们控制器
 */
class Joinus  extends \think\Controller
{
    //加入我们标题接口
    public function index()
    {
        try {
            $menu_id = input('menu_id', '-1', 'htmlspecialchars');//第三个参数过滤
            $language = input("lang", "cn", "htmlspecialchars");
            //当前页数
            //$page=input('page','1','htmlspecialchars');
            //每页显示条数
            //$psize=input('psize','1','htmlspecialchars');
            if ($language == "cn") {
                $menu_name = "menu_name_cn";
                $tab_name = "加入我们";
                $title = "title_cn";
                $location = "location_cn";
            } elseif ($language == "en") {
                $menu_name = "menu_name_en";
                $tab_name = "Join Us";
                $title = "title_en";
                $location = "location_en";

            }
            $data =Db::query("select menu_id,p_id,$menu_name from ls_menu where `menu_id`= {$menu_id} and `delete_time` is null ");
            //var_dump($data);exit;
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data, $menu_id, $language, "menu");
            //echo "<pre>";
             //print_r($head_title);exit;
            //调用kv类查kv画面内容
            $menu = Kv::Kv($menu_id);
            //echo "<pre>";
            //print_r($menu);exit;

            //计算偏移量 分页
           // $offset = ($page - 1)*$psize;
            $content = Db::query("select joinus_id,$title as title, $location as location  from ls_joinus where `delete_time` is null");//limit $offset,$psize
            $last["tab_name"] = $tab_name;
            $last["child"] = $content;
            /*foreach ($last as $k=>$v){
                $v["child"] = $content;
            }*/
            //计算分页总个数
           // $count = Db::query("select count(*) as total from ls_joinus where `delete_time` is null");
            // var_dump($count);exit;
           // foreach ($count as $a=>$b){
               // $page_count = ceil($b['total'] / $psize);
           // }

            //获取面包屑
            $crumbs = Crumbs::crumbs($data, $menu_id, $language, "menu");
            // echo "<pre>";
            //print_r($crumbs);exit;
            //判断记录存不存在
            if (empty($menu)) {
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            } else {
                return json([
                    'code' => 200,
                    'head_title' => $head_title,
                    'KV' => $menu[0],
                    'crumbs' => $crumbs[0],
                    'joinus' => $last,
                   // 'page_count'=>$page_count,
                    "imsg" => "加入我们列表页 "
                ]);
            }
        } catch (\Exception $e) {
	//		$this->_log2db('index::被 catch 了');
            return json([
                'code' => 2333,
                'imsg' => '被catch',
                'message' => '数据库查询失败 ' . $e->getMessage(),
            ]);
        }
    }

    //加入我们详情接口
    public function detail(){
        try{
            $joinus_id = input('joinus_id','-1','htmlspecialchars');//第三个参数过滤
            $language = input("lang","cn","htmlspecialchars");
            if($language=="cn"){
                $menu_name = "menu_name_cn";
                $tab_name = "加入我们";
                $title = "title_cn";
                $location = "location_cn";
                $qualification = "qualification_cn";
                $responsiblities = "responsiblities_cn";

            }elseif($language=="en"){
                $menu_name = "menu_name_en";
                $tab_name = "Join Us";
                $title = "title_en";
                $location = "location_en";
                $qualification = "qualification_en";
                $responsiblities = "responsiblities_en";
            }
            //获取加入我们的内容
            $joinus =  Db::query("select p_id, joinus_id, $title as title,$location as location  from ls_joinus where `joinus_id`={$joinus_id} and `delete_time` is null");
            //获取资格
            $qual = Db::query("select qu_id, p_id,$qualification as qualification  from ls_qualification where `p_id`={$joinus_id} and `delete_time` is null ");

            $resp = Db::query("select re_id, p_id,$responsiblities as responsiblities  from ls_responsiblities where `p_id`={$joinus_id} and `delete_time` is null ");
            //print_r($menu_id);exit;
            //array_merge($qual,$resp);
            //print_r(array_merge($qual,$resp));exit;
            foreach ($joinus as $k => $v) {
                    $joinus[$k]["qualification"] = $qual;
                    $joinus[$k]["responsiblities"] = $resp;
            }

            $last["tab_name"] = $tab_name;
            $last["child"] = $joinus;

//print_r($joinus[0]);exit;
            //调用kv类查kv画面内容
            $menu=Kv::Kv($joinus[0]["p_id"]);
            //print_r($joinus[0]["p_id"]);exit;
            //echo "<pre>";
            //print_r($menu);exit;
            $data =Db::query("select menu_id,p_id,$menu_name from ls_menu where `menu_id`= '{$joinus[0]["p_id"]}' and `delete_time` is null ");
            //调用headtitle查title内容
            $head_title = HeadTitle::title($data,$joinus[0]["p_id"],$language,"menu");

            //获取面包屑
            $crumbs =  Crumbs::crumbs($data,$joinus_id,$language,"joinus");
            //exit;
            // echo "<pre>";
            // print_r($crumbs);exit;
            // return json($crumbs);exit;
            //判断记录存不存在
            if( empty($menu)){
                return json([
                    'code' => 2555,
                    'message' => '不存在这个id',
                ]);
            }else{
                return json([
                    'code' => 200,
                    'head_title'=>$head_title,
                    'KV'=>$menu[0],
                    'crumbs'=>$crumbs,
                    'joinus_detail'=>$last,
                    "imsg" => "加入我们详情页"
                ]);
            }
        }catch (\Exception $e){
//			$this->_log2db('index::被 catch 了');
            return json([
                'code'=>2333,
                'imsg'=>'被catch',
                'message'=>'数据库查询失败'.$e->getMessage(),
            ]);
        }
    }
}
